// pages/classic/classic.js
import {ClassicModel} from "../../modules/classic.js";
import {LikeModel} from "../../modules/like.js";
let classicModel=new ClassicModel();
let likeModel = new LikeModel();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    classic:null,
    first:false, 
    latest: true,  //因为数据获取的就是最新一期  所以默认为true
    likeCount:0,
    likeStatus:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    classicModel.getLatest((res)=>{
      this.setData({
        classic:res,
        likeCount: res.fav_nums,
        likeStatus: res.like_status
      })
    });
  },
  onLike:function(ev){
    likeModel.onlike(ev.detail.behavior,this.data.classic.id,             this.data.classic.type);
  },
  onNext:function(){
  //  console.log("左");
    //当前一期的下一条数据   左是最新
    this._updateClassic("next");

  },
  onPrevious:function(){
  //  console.log("右");
    //当前一期的前一条数据   右是旧的
    this._updateClassic("previous");
  },
  _getLikeStatus:function(artId,category){
      likeModel.getClassicLikeStatus(artId,category,(res=>{
        this.setData({
            likeCount:res.fav_nums,
            likeStatus:res.like_status
        });
      }));
  },
  _updateClassic: function (nextorprevious) {
    let index = this.data.classic.index;
    classicModel.getClassic(index, nextorprevious, (res) => {
   
      this._getLikeStatus(res.id,res.type)
      this.setData({
        classic: res,
        first: classicModel.isFirst(res.index),
        latest: classicModel.isLatest(res.index)
      });
    });
  },
})